// 04jyhs/accountAdmin/withdrawals/withdrawals.js
var co = require('../../../resource/js/common.js')
var Md5 = require('../../../resource/js/md5.js')
var app = getApp()
var _appid,_openid
var _this, _data
Page({
   data: {
      money: '', //余额
      allDeposit: false, //是否全部提现
      overtop: false, //提现金额是否超出余额
      bankList: [],
      inputMoney: '', //输入的金额
      inputStr: '', //输入金额(字符串)
      pay_Fee: '', //手续费
      account_Money: '', //实际到账金额
      modal: false, //选择银行卡模态框
      modal_pay: false, //支付模态框
      indexs: 0, //所选银行卡信息           
   },
   onLoad: function (options) {
      _this = this
      _data = this.data
      _appid = app.Data.app.appid
      _openid = app.Data.userInfo.Wx_Openid[_appid]
      this.setData({
         money: app.Data.shopInfo.Biz_Money
      })
      _this.getBankList() //获取已绑定银行卡信息
   },

   allWithdraw: function () {
      var pay_fee = _data.money * 0.001 > 1 ? _data.money * 0.001 : 1
      this.setData({
         allDeposit: true,
         inputMoney: _data.money,
         inputStr: co.roundStr(_data.money, 2),
         pay_Fee: co.roundStr(pay_fee, 2),
         account_Money: co.roundStr(_data.money - pay_fee, 2)
      })
   },

   //输入金额
   inputMoney: function (e) {
      var balance = parseFloat(this.data.money);
      var money = parseFloat(e.detail.value);
      var inputStr = e.detail.value
      var pay_fee = money * 0.001 > 1 ? money * 0.001 : 1
      this.setData({
         overtop: money > balance,
         allDeposit: false,
         inputMoney: money,
         inputStr: co.filterStr(inputStr, '.'),
         pay_Fee: co.roundStr(pay_fee, 2),
         account_Money: co.roundStr(money - pay_fee, 2)
      })
   },

   //获取已绑定银行卡信息
   getBankList: function (userID, ID_Card_Name) {
      var url = 'bankcard/ba/getList';
      co.getShareApi(url, {
         User_ID: 3950
      }, function (res) {
         if (res.code == 0) {
            _this.setData({
               bankList: res.data
            })
         }
      })
   },

   //显示选择提现方式
   select: function () {
      this.setData({
         modal: true
      })
   },

   //关闭选择提现方式
   close: function () {
      this.setData({
         modal: false
      })
   },
   radioChange: function (e) {
      var index = e.detail.value
      console.log(index)
      if (index > -1) {
         this.setData({
            modal: false,
            indexs: e.detail.value
         })
      }
   },
   //提现(显示支付密码模态框)
   submit: function (e) {
      if (co.empty(this.data.inputMoney) || this.data.inputMoney <= 0) {
         return
      }
      if (_data.indexs > 0 && _data.inputMoney <= 1) {
         co.confirm('', '提现到银行卡的金额需大于1元，因为微信收取手续费最低1元',
            '取消', '确定',
            function (res) {
               _this.setData({
                  allDeposit: false,
                  inputMoney: 0,
                  inputStr: '',
                  pay_Fee: '',
                  account_Money: ''
               })
            })
         return
      }
      //提现
      if (this.data.overtop) {
         wx.showModal({
            title: '',
            content: '您输入的金额超过您的余额',
         })
      } else {
         this.setData({
            inputMoney: co.round(_data.inputMoney, 2),
            inputStr: co.roundStr(_data.inputMoney, 2),
            modal_pay: true
         })
      }
   },
   //关闭支付密码模态框
   payClose: function () {
      this.setData({
         modal_pay: false
      })
   },
   //提现
   withdraw: function (e) {
      wx.showLoading({
         title: '支付中',
      })
      this.setData({
         modal_pay: false
      })
      var Biz_PayPassWord = Md5.md5(e.detail.value.Biz_PayPassWord)
      var bank = _data.bankList[_data.indexs]
      _this.putForwardCord(_data.indexs, Biz_PayPassWord)
   },
   //提现到微信
   payToWeixinMoney: function (Biz_PayPassWord, _openid, name) {
      var shop = app.Data.shopInfo
      co.getShareApi('api/Wxpay/toWxWallet', {
         Biz_ID: shop.Biz_ID,
         Biz_PayPassword: Biz_PayPassWord,
         amount: _data.inputMoney,
         desc: shop.Biz_Account + '-旧约回收提现',
         mch_appid: _appid,
         paySecret: 'bShYjNaUyIzGnEhSiAzIlIsNaHuOhZuF',
         openid: _openid,
         real_name: name,
         mchid: '1514732771',
         Payer: 'User_' + app.Data.userInfo.User_ID,
         Pay_From: '91_pay'
      }, function (res) {
         console.log(res)
         wx.hideLoading()
         if (res.code == 0) {
            co.confirm('', res.msg, '好的!', '朕知道了', function (res) {
               co.navigateBack({}, 1000)
            })
         } else {
            co.Modal(res.msg)
         }
      })
   },
   //提现到银行
   payToBank: function (Biz_PayPassWord) {
      var bank = _data.bankList[_data.indexs]
      var bank_code = bank.Bank_Code
      var bank_Name = bank.Bank_Name
      var enc_bank_no = bank.Account_No
      var enc_true_name = bank.Account_Name
      var shop = app.Data.shopInfo
      var param = {
         Biz_ID: shop.Biz_ID,
         Biz_PayPassword: Biz_PayPassWord,
         mch_appid: _appid,
         paySecret: 'bShYjNaUyIzGnEhSiAzIlIsNaHuOhZuF',
         mch_id: '1514732771',
         bank_code: bank_code,
         amount: _data.inputMoney,
         desc: shop.Biz_Account + '-旧约回收提现(' + bank_Name + '***' + enc_bank_no.substring(enc_bank_no.length - 3) + ')',
         enc_bank_no: enc_bank_no,
         enc_true_name: enc_true_name,
         Payer: 'User_' + app.Data.userInfo.User_ID,
         Pay_From: '91_pay'
      }
      co.getShareApi('api/Wxpay/toBank', param, function (res) {
         wx.hideLoading()
         if (res.code == 0) {
            co.confirm('', res.msg, '好的', '朕知道了', function (res) {
               co.navigateBack({}, 1000)
            })
         } else {
            co.Modal(res.msg)
         }
      })
   },
   //提现添加支付记录
   putForwardCord(index, Biz_PayPassWord) {
      var param = {
         Money: _data.inputMoney,
         Biz_ID: app.Data.shopInfo.Biz_ID,
         Payer: 'Biz_' + app.Data.shopInfo.Biz_ID,
         Pay_From: "91_pay",
      }
      if(index == 0){
         param.Abstract = "提现到微信上"
         param.Pay_To = "wx_pay"
      }else{
         param.Abstract = "提现到银行卡上"
         param.Pay_To = "bank_card"
      }
      co.getJyhsApi("money/Recordbiz/bizAdd", param, function (res) {
         if (res.code == 0) {
            if (index == 0){
               _this.payToWeixinMoney(Biz_PayPassWord, _openid, app.Data.shopInfo.Biz_Contact)
            }else{
            _this.payToBank(Biz_PayPassWord)
            }
         }
      })
   },
})